package com.uenpay.im.ui.old.activity.dynamic;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.qiyunxin.android.http.utils.Utils;
import com.qyx.android.weight.edittext.ExpressionUtil;
import com.uenpay.im.IMApplication;
import com.uenpay.im.R;
import com.uenpay.im.ui.old.activity.chat.ActivityHandle;
import com.uenpay.im.weight.MyDialog;

import java.util.ArrayList;

/**
 * @author SL
 * @created 2016-1-21 8:37:57
 * @copyRight http://www.qiyunxin.com
 * @function TODO
 */
public class DynamicReplyHandler {

    public Context mContext;

    public DynamicReplyHandler(Context _Context) {
        mContext = _Context;
    }

    public void setPraiseView(ArrayList<Praise> praise, LinearLayout praise_ll,
                              TextView praise_tv) {
        if (praise != null && praise.size() > 0) {
            praise_ll.setVisibility(View.VISIBLE);
            String s = "";
            for (int i = 0; i < praise.size(); i++) {
                if (i == praise.size() - 1) {
                    s += praise.get(i).nickname.trim();
                } else {
                    s += praise.get(i).nickname.trim() + ",";
                }
            }
            s = Utils.ToDBC(s);
            praise_tv.setText(s);
        } else {
            praise_ll.setVisibility(View.GONE);
        }
    }

    public View getReplyTextView(int dyPos, EditText _editText, Reply r,
                                 int deleteReplyIndx, LinearLayout faLin,
                                 ITextLinkListener mLinkListener) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_reply,
                new RelativeLayout(mContext), false);
        TextView reply_all_tv = (TextView) view.findViewById(R.id.reply_all_tv);
        SpannableStringBuilder ssb = new SpannableStringBuilder();
        String reply = mContext.getResources()
                .getString(R.string.dynamic_reply);
        if (r.pcustid == 0) {
            ssb.append(r.nickname.trim() + ": ");
        } else {
            ssb.append(r.nickname.trim() + reply + r.pnickname.trim() + ": ");
        }
        TextLinkListener tllf = new TextLinkListener(dyPos, deleteReplyIndx,
                _editText, 0, r, faLin, 0, mLinkListener);

        TextLinkListener tllr = new TextLinkListener(dyPos, deleteReplyIndx,
                _editText, 0, r, faLin, r.pcustid, mLinkListener);
        ssb.setSpan(tllf, 0, r.nickname.length(),
                Spanned.SPAN_INCLUSIVE_INCLUSIVE);

        ssb.append(ExpressionUtil.getExpressionString(mContext, r.content
                .trim().replace(" ", "")));

        TextLinkListener tlll = new TextLinkListener(dyPos, deleteReplyIndx,
                _editText, 2, r, faLin, 0, mLinkListener);

        if (r.pcustid != 0) {

            ssb.setSpan(new ForegroundColorSpan(Color.BLACK),
                    r.nickname.length(), r.nickname.length() + reply.length(),
                    Spanned.SPAN_INCLUSIVE_INCLUSIVE);
            ssb.setSpan(
                    tllr,
                    r.nickname.length() + reply.length(),
                    r.nickname.length() + r.pnickname.length() + reply.length(),
                    Spanned.SPAN_INCLUSIVE_INCLUSIVE);
            ssb.setSpan(tlll, r.nickname.length() + reply.length()
                            + r.pnickname.length() + 1, ssb.length(),
                    Spanned.SPAN_INCLUSIVE_INCLUSIVE);

        } else {

            ssb.setSpan(tlll, r.nickname.length() + 1, ssb.length(),
                    Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        }

        reply_all_tv.setMovementMethod(LinkMovementMethod.getInstance());
        reply_all_tv.setText(ssb);
        return view;

    }

    public interface ITextLinkListener {
        void onTextLink(int dypos, int status, String custId,
                        String pCustId, int deleteReplyIndx, LinearLayout linearLayout);
    }

    ;

    public class TextLinkListener extends ClickableSpan {
        private int type;
        private Reply r;
        private int dyPos;
        private int source_id;
        private EditText editText;
        private ITextLinkListener mLinkListener;
        private int deleteReplyIndx = 0;
        private LinearLayout linearLayout;

        public TextLinkListener(int _dyPos, int _deleteReplyIndx,
                                EditText _editText, int type, Reply reply,
                                LinearLayout _linearLayout, int source_cust_id,
                                ITextLinkListener _LinkListener) {
            this.type = type;
            this.dyPos = _dyPos;
            this.r = reply;
            this.source_id = source_cust_id;
            this.mLinkListener = _LinkListener;
            this.editText = _editText;
            this.deleteReplyIndx = _deleteReplyIndx;
            this.linearLayout = _linearLayout;
        }

        @Override
        public void onClick(View widget) {
            switch (type) {
                case 0:
                    if (source_id == 0) {
                        ActivityHandle.startUserDetailActivity((Activity) mContext,
                                r.custid);
                    } else {
                        ActivityHandle.startUserDetailActivity((Activity) mContext,
                                r.pcustid + "");
                    }

                    break;
                case 1:
                    break;
                case 2:
                    final String runame = r.nickname;
                    final String rid = r.replyid;
                    if (r.custid.equals(IMApplication.getCustId())) {

                        MyDialog.Builder alertDialog = new MyDialog.Builder(
                                mContext);
                        alertDialog.setTitle(R.string.dialog_title);
                        alertDialog.setMessage(mContext.getResources().getString(
                                R.string.delete_dynamic_dialog));
                        alertDialog.setPositiveButton(R.string.sure,
                                new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog,
                                                        int which) {
                                        dialog.dismiss();
                                        mLinkListener.onTextLink(dyPos, 0, rid,
                                                source_id + "", deleteReplyIndx,
                                                linearLayout);
                                    }
                                });

                        alertDialog
                                .setNegativeButton(
                                        R.string.cancel,
                                        new DialogInterface.OnClickListener() {
                                            public void onClick(
                                                    DialogInterface dialog,
                                                    int which) {
                                                dialog.dismiss();
                                            }
                                        });
                        alertDialog.create().show();

                    } else {
                        editText.setHint(mContext.getResources()
                                .getString(R.string.dynamic_reply).trim()
                                + runame.trim() + ":");
                        mLinkListener.onTextLink(dyPos, 1, rid, r.custid + "",
                                deleteReplyIndx, linearLayout);
                    }

                    break;
                default:
                    break;
            }
        }

        @Override
        public void updateDrawState(TextPaint ds) {
            if (type == 0) {
                ds.setColor(mContext.getResources().getColor(
                        R.color.reply_name_color));
            } else {
                ds.setColor(mContext.getResources().getColor(
                        R.color.the_color_black));
            }
            ds.setTextSize(mContext.getResources().getDimension(
                    R.dimen.font_size_14sp));
            ds.setUnderlineText(false);
        }
    }
}
